import 'package:base_flutter_project/plugin/asr_manager.dart';
import 'package:flutter/material.dart';

const double MIC_SIZE = 80;

class SpeakPage extends StatefulWidget {
  @override
  _SpeakPageState createState() => _SpeakPageState();
}

class _SpeakPageState extends State<SpeakPage>
    with SingleTickerProviderStateMixin {
  Animation<double> _animation;
  AnimationController _animationController;
  String _finalval = "识别内容";
  @override
  void initState() {
    _animationController =
        AnimationController(vsync: this, duration: Duration(milliseconds: 500));
    _animation =
        CurvedAnimation(parent: _animationController, curve: Curves.easeIn)
          ..addStatusListener((status) {
            if (status == AnimationStatus.completed) {
              _animationController.reverse();
            } else if (status == AnimationStatus.dismissed) {
              _animationController.forward();
            }
          });
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _animationController.dispose();
    super.dispose();
  }

  _stop() {
    _animationController.reset();
    _animationController.stop();
    AsrManager.stop().then((res){
      setState(() {
        _finalval = res;
      });
    });
  }

  _start() {
    _animationController.forward();
    AsrManager.start().then((res){
      print(res);
      setState(() {
        _finalval = res;
      });
    }).catchError((e){
      print(e);
      setState(() {
        _finalval = e.toString();
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;

    return Scaffold(
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            Navigator.of(context).pop();
          },
          child: Icon(Icons.arrow_back),
        ),
        body: Stack(
            children: <Widget>[
              Positioned(
                bottom: 90,
                width: size.width,
                child: Container(
                  alignment: Alignment.center,
                  child: Text("长按说话"),
                ),
              ),
              Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.all(60.0),
                      child: Text("$_finalval"),
                    ),
                    GestureDetector(
                      onTapDown: (e) {
                        _start();
                      },
                      onTapUp: (e) {
                        _stop();
                      },
                      onTapCancel: () {},
                      child: AnimateMic(
                        animation: _animation,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),);
  }
}

class AnimateMic extends AnimatedWidget {
  static final _opacityTeen = Tween<double>(begin: 1, end: 0.5);
  static final _sizeTween = Tween<double>(begin: MIC_SIZE, end: MIC_SIZE - 20);

  AnimateMic({Key key, Animation<double> animation})
      : super(key: key, listenable: animation);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    final Animation<double> animation = listenable;
    return Opacity(
      opacity: _opacityTeen.evaluate(animation),
      child: Container(
        height: _sizeTween.evaluate(animation),
        width: _sizeTween.evaluate(animation),
        decoration: BoxDecoration(
            color: Colors.blue,
            borderRadius: BorderRadius.circular(MIC_SIZE / 2.0)),
        child: Icon(
          Icons.mic,
          color: Colors.white,
          size: 30,
        ),
      ),
    );
  }
}
